<?php
class PostsController extends AppController {

	var $name = 'Posts';

	function index() {
		$this->Post->recursive = 0;
		$this->set('posts', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid post', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('post', $this->Post->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Post->create();
			if ($this->Post->save($this->data)) {
				$this->Session->setFlash(__('The post has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The post could not be saved. Please, try again.', true));
			}
		}
		$categories = $this->Post->Category->find('list');
		$users = $this->Post->User->find('list');
		$this->set(compact('categories', 'users'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid post', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Post->save($this->data)) {
				$this->Session->setFlash(__('The post has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The post could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Post->read(null, $id);
		}
		$categories = $this->Post->Category->find('list');
		$users = $this->Post->User->find('list');
		$this->set(compact('categories', 'users'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for post', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Post->delete($id)) {
			$this->Session->setFlash(__('Post deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Post was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
	
	function admin_index() {
		$this->Post->recursive = 0;
		$this->set('posts', $this->paginate());
	}

	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid post', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('post', $this->Post->read(null, $id));
	}

	function admin_add() {
		if (!empty($this->data)) {
			$this->Post->create();
			if ($this->Post->save($this->data)) {
				$this->Session->setFlash(__('The post has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The post could not be saved. Please, try again.', true));
			}
		}
		$categories = $this->Post->Category->find('list');
		$users = $this->Post->User->find('list');
		$this->set(compact('categories', 'users'));
	}

	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid post', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Post->save($this->data)) {
				$this->Session->setFlash(__('The post has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The post could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Post->read(null, $id);
		}
		$categories = $this->Post->Category->find('list');
		$users = $this->Post->User->find('list');
		$this->set(compact('categories', 'users'));
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for post', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Post->delete($id)) {
			$this->Session->setFlash(__('Post deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Post was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
	/**
	* ra = requestAction: for call from view directly
	*/
	/**
	*	return the 5 latest announcements 
	*/
	function ra_latest_posts(){
		return $this->Post->find('all', array(
			'condition' => array('Post.published' => 'YES'),
			'recursive' => 0, 
			'order' => array('Post.modified' => 'DESC'), 
			'limit' => 5
		));
	}
	/**
	*	return the most popular announcements + posts
	*/
	function ra_populars(){
		$this->loadModel('Announcement');
		$announcements = $this->Announcement->find('all', array(
			'condition' => array('Announcement.published' => 'YES'),
			'recursive' => 0,
			'order' => array('Announcement.read' => 'DESC'),
			'field' => array('Announcement.id', 'Announcement.title', 'Announcement.read'),
			'limit' => 5
		));
		$posts = $this->Post->find('all', array(
			'condition' => array('Post.published' => 'YES'),
			'recursive' => 0,
			'order' => array('Post.read' => 'DESC'),
			'field' => array('Post.id', 'Post.title', 'Post.read'),
			'limit' => 5
		));
		$populars = array('announces' => $announcements, 'pos' => $posts);
		return $populars;
	
	}
	/**
	*	return the 3 latest posts in the same category
	*/
	function ra_latest_posts_category($category_id){
		if(isset($category_id)){
			return $this->Post->find('all', array(
				'conditions' => array('Post.published' => 'YES', 'Post.category_id' => $category_id),
				'order' => array('Post.created' => 'DESC'),
				'limit' => 3,
				'recursive' => 0
			));
		}
		return null;
	}
}
?>